home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 4 / ETO Development Tools 4.iso / Tools - Objects / MacApp / MacApp 3.0a2 / Libraries / UFileBasedDocument.cp < prev    next >
Encoding:
Text File  |  1991-05-01  |  11.2 KB  |  469 lines  |  [TEXT/MPS ]

  1. // UFileBasedDocument.cp 
  2. // Copyright © 1984-1991 by Apple Computer Inc.    All rights reserved.
  3.  
  4. #ifndef __UFILEBASEDDOCUMENT__
  5. #include <UFileBasedDocument.h>
  6. #endif
  7.  
  8. #ifndef __GEOMETRY__
  9. #include <Geometry.h>
  10. #endif
  11.  
  12. #ifndef __RESOURCES__
  13. #include <Resources.h>
  14. #endif
  15.  
  16. #ifndef __TOOLUTILS__
  17. #include <ToolUtils.h>
  18. #endif
  19.  
  20. #ifndef __ERRORS__
  21. #include <Errors.h>
  22. #endif
  23.  
  24. #ifndef __STANDARDFILE__
  25. #include <StandardFile.h>
  26. #endif
  27.  
  28. #ifndef __ULIST__
  29. #include <UList.h>
  30. #endif
  31.  
  32. #ifndef __UFILE__
  33. #include <UFile.h>
  34. #endif
  35.  
  36. #ifndef __UVIEW__
  37. #include <UView.h>
  38. #endif
  39.  
  40. #ifndef __UFILEHANDLER__
  41. #include <UFileHandler.h>
  42. #endif
  43.  
  44. #ifndef __UPRINTHANDLER__
  45. #include <UPrintHandler.h>
  46. #endif
  47.  
  48. #ifndef __UMACAPPUTILITIES__
  49. #include <UMacAppUtilities.h>
  50. #endif
  51.  
  52. #ifndef __UERRORMGR__
  53. #include <UErrorMgr.h>
  54. #endif
  55.  
  56. #ifndef __UMACAPPGLOBALS__
  57. #include <UMacAppGlobals.h>
  58. #endif
  59.  
  60. #ifndef __UMEMORY__
  61. #include <UMemory.h>
  62. #endif
  63.  
  64. #ifndef __UITERATOR__
  65. #include <UIterator.h>
  66. #endif
  67.  
  68. //--------------------------------------------------------------------------------------------------
  69. #pragma segment MAOpen
  70.  
  71. pascal void TFileBasedDocument::Initialize(void)// override 
  72. {
  73.     inherited::Initialize();
  74.  
  75.     fFileHandler = NULL;
  76.     fScrapType = '    ';                            // need constant kNoScrapType
  77. }
  78.  
  79. //--------------------------------------------------------------------------------------------------
  80. #pragma segment MAOpen
  81.  
  82. pascal void TFileBasedDocument::IFileBasedDocument(TFile* itsFile,
  83.                                                    const OSType itsFileType,
  84.                                                    const OSType itsCreator,
  85.                                                    const OSType itsScrapType,
  86.                                                    Boolean usesDataFork,
  87.                                                    Boolean usesRsrcFork,
  88.                                                    Boolean keepsDataOpen,
  89.                                                    Boolean keepsRsrcOpen)
  90. {
  91.     this->IDocument();
  92.  
  93.     fFileHandler = this->DoMakeFileHandler(itsFile, itsFileType, itsCreator, usesDataFork, usesRsrcFork, keepsDataOpen, keepsRsrcOpen);
  94.     fScrapType = itsScrapType;
  95. }
  96.  
  97. //--------------------------------------------------------------------------------------------------
  98. #pragma segment MAOpen
  99.  
  100. pascal TFileHandler* TFileBasedDocument::DoMakeFileHandler(TFile* itsFile,
  101.                                                            const OSType itsFileType,
  102.                                                            const OSType itsCreator,
  103.                                                            Boolean usesDataFork,
  104.                                                            Boolean usesRsrcFork,
  105.                                                            Boolean keepsDataOpen,
  106.                                                            Boolean keepsRsrcOpen)
  107. {
  108.     TFileHandler * aFileHandler;
  109.  
  110.     aFileHandler = new TFileHandler;
  111.     aFileHandler->IFileHandler(this, itsFile, itsFileType, itsCreator, usesDataFork, usesRsrcFork, keepsDataOpen, keepsRsrcOpen);
  112.     return aFileHandler;
  113. }
  114.  
  115. //--------------------------------------------------------------------------------------------------
  116. #pragma segment MAClose
  117.  
  118. pascal void TFileBasedDocument::Free(void)        // override 
  119. {
  120.     fFileHandler = (TFileHandler *)FreeIfObject(fFileHandler);
  121.  
  122.     inherited::Free();
  123. }
  124.  
  125. //--------------------------------------------------------------------------------------------------
  126. #pragma segment MAFile
  127.  
  128. pascal Boolean TFileBasedDocument::AlreadyOpen(TFile* aFile)// override 
  129. {
  130.     return fFileHandler->FileAlreadyOpen(aFile);
  131. }
  132.  
  133. //--------------------------------------------------------------------------------------------------
  134. #pragma segment MAFile
  135.  
  136. pascal void TFileBasedDocument::CheckFile(short rsrcId,
  137.                                           short rsrcIndex,
  138.                                           Boolean reverting)
  139. {
  140.     OSErr err;
  141.     Str255 name;
  142.     Str255 s;
  143.  
  144.     err = fFileHandler->FileChanged(reverting);    // don't care about the file type if saving 
  145.     if (err == errFileChanged)
  146.     {
  147.         name = fTitle;
  148.         GetIndString(s, rsrcId, rsrcIndex);
  149.         ParamText(name, s, "", "");
  150.         if (MacAppAlert(phFileChanged, NULL) == cancel)// !!! This should be programatically defeatable 
  151.             Failure(noErr, msgCancelled);
  152.     }
  153.     else if ((err != noErr) && reverting)
  154.         Failure(err, 0);
  155. }
  156.  
  157. //--------------------------------------------------------------------------------------------------
  158. #pragma segment MAWriteFile
  159.  
  160. pascal void TFileBasedDocument::DoNeedDiskSpace(TFile* ,
  161.                                                 long& dataForkBytes,
  162.                                                 long& rsrcForkBytes)
  163. {
  164.     if (fSavePrintInfo)
  165.     {
  166.         if (fFileHandler->HasRsrcFork())
  167.             rsrcForkBytes = rsrcForkBytes + GetHandleSize(fPrintInfo);// don't assume size 
  168.         else
  169.             dataForkBytes = dataForkBytes + kPrintInfoSize;
  170.     }
  171. }
  172.  
  173. //--------------------------------------------------------------------------------------------------
  174. #pragma segment MAReadFile
  175.  
  176. pascal void TFileBasedDocument::DoRead(TFile* aFile,
  177.                                        Boolean forPrinting)
  178. {
  179.     if (fSavePrintInfo)
  180.         this->DoReadPrintInfo(aFile, forPrinting);
  181. }
  182.  
  183. //--------------------------------------------------------------------------------------------------
  184. #pragma segment MAReadFile
  185.  
  186. pascal void TFileBasedDocument::DoReadPrintInfo(TFile* aFile,
  187.                                                 Boolean)
  188. {
  189.     Handle hPrintInfo;
  190.  
  191.     if (fFileHandler->FileExists())
  192.     {
  193.         if (aFile->IsRsrcForkOpen())
  194.         {
  195.             hPrintInfo = GetResource(kPrintInfoRsrcType, kPrintInfoRsrcID);// Read the print info resource 
  196.             FailNILResource(hPrintInfo);
  197.             if (!fPrintInfo)
  198.             {
  199.                 DetachResource(hPrintInfo);
  200.                 fPrintInfo = hPrintInfo;
  201.             }
  202.             else
  203.             {
  204.                 BlockMove(*hPrintInfo, *fPrintInfo, GetHandleSize(fPrintInfo));
  205.                 ReleaseResource(hPrintInfo);
  206.             }
  207.         }
  208.         else
  209.         {
  210.             long count = kPrintInfoSize;
  211.             SignedByte savedState;
  212.  
  213.             if (!fPrintInfo)
  214.                 fPrintInfo = NewPermHandle(kPrintInfoSize);
  215.  
  216.             savedState = LockHandleHigh(fPrintInfo);
  217.             FailOSErr(aFile->ReadData((*fPrintInfo), count));
  218.             HSetState(fPrintInfo, savedState);
  219.         }
  220.     }
  221. }
  222.  
  223. //--------------------------------------------------------------------------------------------------
  224. #pragma segment MAWriteFile
  225.  
  226. pascal void TFileBasedDocument::AboutToSaveFile(CmdNumber itsCmd,
  227.                                                 Boolean& makingCopy)
  228. {
  229. }
  230.  
  231. //--------------------------------------------------------------------------------------------------
  232. #pragma segment MAWriteFile
  233.  
  234. pascal void TFileBasedDocument::DoWrite(TFile* aFile,
  235.                                         Boolean makingCopy)
  236. {
  237.     if (fSavePrintInfo)
  238.         this->DoWritePrintInfo(aFile, makingCopy);
  239. }
  240.  
  241. //--------------------------------------------------------------------------------------------------
  242. #pragma segment MAWriteFile
  243.  
  244. pascal void TFileBasedDocument::DoWritePrintInfo(TFile* aFile,
  245.                                                  Boolean)
  246. {
  247.     long count;
  248.  
  249.     if (fPrintInfo == NULL)
  250.     {
  251. #if qDebug
  252.         ProgramBreak("no print record in document");
  253. #endif
  254.  
  255.     }
  256.     else
  257.     {
  258.         Handle tempHandle;
  259.  
  260.         if (aFile->IsRsrcForkOpen())
  261.         {
  262.             tempHandle = fPrintInfo;
  263.             FailOSErr(HandToHand(tempHandle));
  264.             AddResource(tempHandle, kPrintInfoRsrcType, kPrintInfoRsrcID, "");
  265.             FailResError();
  266.         }
  267.         else
  268.         {
  269.             count = kPrintInfoSize;
  270.             FailOSErr(aFile->WriteData(*fPrintInfo, count));
  271.         }
  272.     }
  273. }
  274.  
  275. //--------------------------------------------------------------------------------------------------
  276. #pragma segment MADocumentRes
  277.  
  278. pascal void TFileBasedDocument::CloseFile(void)
  279. {
  280.     fFileHandler->CloseFile();
  281. }
  282.  
  283. //--------------------------------------------------------------------------------------------------
  284. #pragma segment MAReadFile
  285.  
  286. pascal void TFileBasedDocument::ReadDocument(Boolean forPrinting)// override 
  287. {
  288.     Str63 fileName;
  289.  
  290.     fFileHandler->ReadFile(forPrinting);
  291.     fFileHandler->GetFileName(fileName);
  292.     fTitle = fileName;
  293.     this->SetChangeCount(0);
  294. }
  295.  
  296. //--------------------------------------------------------------------------------------------------
  297. #pragma segment MAReadFile
  298.  
  299. pascal void TFileBasedDocument::RevertDocument(void)
  300. {
  301.     FailInfo fi;
  302.  
  303.     if (fi.Try())
  304.     {
  305.         TCommand * lastCommand;
  306.  
  307.         this->CheckFile(kIDBuzzString, bzRevertAnyways, TRUE);
  308.  
  309.         lastCommand = this->GetLastCommand();
  310.         if ((lastCommand != NULL) && (lastCommand->fChangedObject == this))
  311.             this->CommitLastCommand();
  312.  
  313.         this->FreeData();
  314.  
  315.         if (fFileHandler->FileExists())
  316.             fFileHandler->ReadFile(kForDisplay);
  317.         else
  318.         {
  319.             CObjectIterator iter(fViewList);
  320.         
  321.             for (TView* view = (TView*) iter.FirstObject(); iter.More(); view = (TView*) iter.NextObject())
  322.                 if (view->fPrintHandler)
  323.                     view->fPrintHandler->Reset();
  324.  
  325.             this->DoInitialState();
  326.         }
  327.  
  328.         this->SetChangeCount(0);
  329.         fi.Success();
  330.     }
  331.     else    // Recover
  332.     {
  333.         if (fi.error == fnfErr)
  334.             fi.error = errRevertFNF;
  335.         if (fi.message == 0)
  336.             gErrorParm3 = fTitle;
  337.         FailNewMessage(fi.error, fi.message, msgRevertFailed);
  338.         fi.ReSignal();
  339.     }
  340. }
  341.  
  342. //--------------------------------------------------------------------------------------------------
  343. #pragma segment MAWriteFile
  344.  
  345. pascal void TFileBasedDocument::SaveDocument(CmdNumber itsCmdNumber)// override 
  346. {
  347.     TCommand * lastCommand;
  348.     Boolean askForFilename =!fFileHandler->FileExists() || ((itsCmdNumber != cSave) && (itsCmdNumber != cClose));
  349.     Boolean makingCopy = itsCmdNumber == cSaveCopy;
  350.     Boolean copyFInfo =!(askForFilename || makingCopy);
  351.  
  352.     if (copyFInfo)
  353.         this->CheckFile(kIDBuzzString, bzSaveAnyways, FALSE);
  354.  
  355.     lastCommand = this->GetLastCommand();
  356.     if (fCommitOnSave || (!makingCopy) && (lastCommand != NULL) && (lastCommand->fChangedObject == this))
  357.         this->CommitLastCommand();
  358.  
  359.     fFileHandler->SaveFile(itsCmdNumber, askForFilename, copyFInfo, makingCopy);
  360. }
  361.  
  362. //--------------------------------------------------------------------------------------------------
  363. #pragma segment MAWriteFile
  364.  
  365. pascal void TFileBasedDocument::FileHasBeenSaved(const Str255& newName)
  366. {
  367.     this->SetChangeCount(0);
  368.  
  369.     if (fTitle != newName)
  370.         this->SetTitle(newName);
  371. }
  372.  
  373. //--------------------------------------------------------------------------------------------------
  374. #pragma segment MAWriteFile
  375.  
  376. pascal void TFileBasedDocument::SFPutParms(CmdNumber itsCmdNumber,
  377.                                            Str255& prompt,
  378.                                            Str255& ,
  379.                                            short& dlgID,
  380.                                            Point& where,
  381.                                            ProcPtr& dlgHook,
  382.                                            ProcPtr& modalFilter,
  383.                                            Ptr& activeList,
  384.                                            ProcPtr& activateProc,
  385.                                            Ptr& yourDataPtr)
  386. {
  387.     short idx;
  388.  
  389.     if (qNeedsAliasMgr || gConfiguration.hasAliasMgr)
  390.     {
  391.         dlgID = sfPutDialogID;
  392.         SetPt(where, -1, -1);
  393.     }
  394.     else
  395.     {
  396.         DialogTHndl dlogTemplate;
  397.         Rect dialogRect;
  398.  
  399.         dlgID = putDlgID;                        // putDlgID defined by Standard File 
  400.         dlogTemplate = (DialogTHndl) GetResource('DLOG', dlgID);
  401.         if (dlogTemplate)
  402.         {
  403.             dialogRect = (*dlogTemplate)->boundsRect;
  404.             CenterRectOnScreen(dialogRect, TRUE, TRUE, TRUE);
  405.             where = dialogRect[topLeft];
  406.         }
  407.         else
  408.             SetPt(where, 100, 100);
  409.     }
  410.  
  411.     switch (itsCmdNumber)
  412.     {
  413.         case cSave:
  414.         case cSaveAs:
  415.             idx = bzSaveAs;
  416.             break;
  417.             
  418.         case cSaveCopy:
  419.             idx = bzSaveCopy;
  420.             break;
  421.             
  422.         default:
  423.             idx = 0;
  424.             break;
  425.     }
  426.  
  427.     if (idx == 0)
  428.         prompt = "";
  429.     else
  430.         GetIndString(prompt, kIDBuzzString, idx);
  431.  
  432.     dlgHook = NULL;
  433.     modalFilter = NULL;
  434.     activeList = NULL;
  435.     activateProc = NULL;
  436.     yourDataPtr = NULL;
  437. }
  438.  
  439. //--------------------------------------------------------------------------------------------------
  440. #pragma segment MADocumentRes
  441.  
  442. pascal TFileHandler* TFileBasedDocument::GetFileHandler(void)
  443. {
  444.     return fFileHandler;
  445. }
  446.  
  447. //--------------------------------------------------------------------------------------------------
  448. #pragma segment MADocumentRes
  449.  
  450. pascal void TFileBasedDocument::SetTitle(const Str255& aTitle)
  451. {
  452.     inherited::SetTitle(aTitle);
  453.  
  454.     fFileHandler->SetFileName(aTitle);
  455. }
  456.  
  457. //--------------------------------------------------------------------------------------------------
  458. #pragma segment MAFields
  459.  
  460. pascal void TFileBasedDocument::Fields(TObject* obj)
  461. {
  462.     obj->DoToField("TFileBasedDocument", (Ptr)NULL, bClass);
  463.     obj->DoToField("fFileHandler", (Ptr) & fFileHandler, bObject);
  464.  
  465.     inherited::Fields(obj);
  466. }
  467.  
  468.  
  469.